package com.jinniulive.platform.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jinniulive.platform.R;
import com.jinniulive.platform.bean.LiveListInfo;

import java.util.List;

/**
 * Created by Administrater on 2016/12/6.
 * 直播列表适配器
 */

public class LiveListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<LiveListInfo.RoomimgdataBean.RoomDataBean> mData;
    private Context mContext;
    private OnRecyclerViewItemClick mListener;

    public interface OnRecyclerViewItemClick {
        void onRcyclerViewItemClick(int position, View view);
    }

    public void setOnRecyclerViewItemClick(OnRecyclerViewItemClick listener) {
        if (listener instanceof OnRecyclerViewItemClick) {
            mListener = listener;
        }
    }

    public LiveListAdapter(Context context) {
        mContext = context;
    }

    public void setmData(List<LiveListInfo.RoomimgdataBean.RoomDataBean> data) {
        if (data != null) {
            mData = data;
            notifyDataSetChanged();
        }
    }

    @Override
    //创建viewHolder
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new BodyHolder(LayoutInflater.from(mContext).inflate(R.layout.item_live_list, parent, false));

    }

    @Override
    //绑定数据
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        LiveListInfo.RoomimgdataBean.RoomDataBean roomDataBean = mData.get(position);
        String roomPic = roomDataBean.getRoomPic();
        Glide.with(mContext).load(roomPic).centerCrop().into(((BodyHolder) holder).ivLiveLogo);
        ((BodyHolder) holder).tvLiveTitle.setText(roomDataBean.getRoomName());
        ((BodyHolder) holder).tvLiveId.setText("ID:" + String.valueOf(roomDataBean.getRoomID()));
        int userOnline = roomDataBean.getUserOnline();
        float s = userOnline >> 4;
        String result = String.format("%.1f", s);
        ((BodyHolder) holder).tvLiveUserOnline.setText(result + "万");
        int onlineTick = roomDataBean.getOnlineTick();
        float s2 = onlineTick >> 4;
        String result2 = String.format("%.1f", s2);
        ((BodyHolder) holder).tvLiveOnlineTick.setText(result2 + "万");
    }

    @Override
    //获取条数目
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    class BodyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView ivLiveLogo;
        private TextView tvLiveTitle;
        private TextView tvLiveId;
        private TextView tvLiveUserOnline;//在线人数
        private TextView tvLiveOnlineTick;//访问量

        BodyHolder(View itemView) {
            super(itemView);
            ivLiveLogo = (ImageView) itemView.findViewById(R.id.iv_live_item_logo);
            tvLiveTitle = (TextView) itemView.findViewById(R.id.tv_item_roomName);
            tvLiveId = (TextView) itemView.findViewById(R.id.tv_item_RoomId);
            tvLiveUserOnline = (TextView) itemView.findViewById(R.id.tv_onLineNum);
            tvLiveOnlineTick = (TextView) itemView.findViewById(R.id.tv_onLineTick);
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            itemView.setOnClickListener(this);
            if (mListener != null) {
                mListener.onRcyclerViewItemClick(position, itemView);
            }
        }
    }

}
